package cn.csq;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:csq02198031@alibaba-inc.com">chengshanqing</a>
 * @date 2025/10/23 11:12
 */
public class WaitNotify {
    static boolean waitFlag = true;
    static Object lock = new Object();
    static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public static void main(String[] args) throws InterruptedException {
        new Thread(new WaitClass(), "wait thread").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(new NotifyClass(), "notify thread").start();
    }


    static class WaitClass implements Runnable {
        @Override
        public void run() {
            System.out.println("【wait】" + "wait thread run");
            synchronized (lock) {
                System.out.println("【wait】获取到锁，执行业务,准备等待");
                // 需要等待时
                while (waitFlag) {
                    try {
                        System.out.println("【wait】执行业务");
                        System.out.println("【wait】" + Thread.currentThread() + " wait flag=" + waitFlag + " time=" + simpleDateFormat.format(System.currentTimeMillis()) + " Thread stats=" + Thread.currentThread().getState());
                        System.out.println("【wait】准备进入等待");
                        lock.wait();
                        System.out.println("【wait】等待结束");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("【wait】" + Thread.currentThread() + " wait flag=" + waitFlag + " time=" + simpleDateFormat.format(System.currentTimeMillis()) + " Thread stats=" + Thread.currentThread().getState());
            System.out.println("【wait】释放锁,准备结束业务流程");

        }
    }

    static class NotifyClass implements Runnable {
        @Override
        public void run() {
            System.out.println("【notify】" + "notify thread run");
            synchronized (lock) {
                try {
                    System.out.println("【notify】获取到锁");
                    System.out.println("【notify】" + Thread.currentThread() + " wait flag=" + waitFlag + " time=" + simpleDateFormat.format(System.currentTimeMillis()) + " Thread stats" + Thread.currentThread().getState());
                    System.out.println("【notify】准备通知其他线程");
                    lock.notifyAll();
                    waitFlag = false;
                    TimeUnit.SECONDS.sleep(5);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            synchronized (lock) {
                System.out.println("【notify】" + Thread.currentThread() + " wait flag=" + waitFlag + " time=" + simpleDateFormat.format(System.currentTimeMillis()) + " Thread stats=" + Thread.currentThread().getState());
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("【notify】" + Thread.currentThread() + " wait flag" + waitFlag + " time " + simpleDateFormat.format(System.currentTimeMillis()) + " Thread stats=" + Thread.currentThread().getState());
        }
    }

}


